/**
 * Copyright 2010 Creative Works, Inc.
 * Creative Works licenses this file to you under the Apache License, version
 * 2.0 (the "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *    http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied.  See the License for the specific language governing
 * permissions and limitations under the License.
 */
package org.sendvox.freswitch.util;

import java.util.ResourceBundle;

public class BundleUtilities {

	/**
	 * Returns the package name of the specified class. An empty String is
	 * returned, if the class is in the default package.
	 */
	public static String getPackageName(
			@SuppressWarnings("rawtypes") Class clazz) {
		String fullClassName = clazz.getName();
		int lastDotIndex = fullClassName.lastIndexOf('.');
		if (lastDotIndex < 0) {
			return ""; // NOI18N
		}
		return fullClassName.substring(0, lastDotIndex);
	}

	/**
	 * Returns the resourcename for the resource' shortName relative to the
	 * classInSamePackage.
	 */
	public static String getResourceName(
			@SuppressWarnings("rawtypes") Class classInSamePackage,
			String shortName) {
		String packageName = getPackageName(classInSamePackage);
		String resourceName = packageName.replace('.', '/') + '/' + shortName;
		return resourceName;
	}

	/**
	 * Returns the resource bundle for the specified resource' shortName
	 * relative to classInSamePackage.
	 */
	public static ResourceBundle getResourceBundle(
			@SuppressWarnings("rawtypes") Class classInSamePackage,
			String shortName) {
		String resourceName = getResourceName(classInSamePackage, shortName);
		return ResourceBundle.getBundle(resourceName);
	}
}
